package com.buss.admin.controller;

import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.admin.service.ICompanyService;
import com.buss.admin.trans.form.CompanyForm;
import com.buss.common.entity.HxCompany;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.util.IDUtils;

@Controller
@RequestMapping("/admin/company")
public class CompanyController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/companylist")
	@ResponseBody
	public EasyUIDataGridResult getCompanyList(CompanyForm companyForm) {

		// 分页查询
		CriteriaQuery cq = companyForm.getCriteriaQuery();
		int pageNo = Integer.parseInt(companyForm.getPage());
		int pageSize = Integer.parseInt(companyForm.getRows());
		// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		PageMode<HxCompany> pageMode = companyService.list4page(cq, pageNo, pageSize);

		// 封装数据
		result.setRows(pageMode.getResult());
		result.setTotal(pageMode.getTotal());
		return result;

	}

	@RequestMapping("/addcompany")
	@ResponseBody
	public HXResult addCompany(HxCompany company) {

		String id = IDUtils.getNumStringId();
		company.setId(id);
		try {
			companyService.saveOrUpdate(company);
			return HXResult.ok("添加信息成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "系统繁忙，添加信息失败！");
		}

	}

	@RequestMapping("/updatecompany")
	@ResponseBody
	public HXResult updateCompany(HxCompany company) {
		try {
			companyService.saveOrUpdate(company);
			return HXResult.ok("修改信息成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，修改信息失败！");
		}

	}

	@RequestMapping("deletecompany")
	@ResponseBody
	public HXResult deleteCompany(String ids) {
		String[] idString = ids.split(",");
		try {
			for (String id : idString) {
				HxCompany hxCompany = companyService.get(HxCompany.class, id);

				companyService.delete(hxCompany);
			}

			return HXResult.ok("删除信息成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，删除信息失败！");
		}
	}
	
	/*
	 * 角色相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		System.out.println("path:" + path);
		return "admin/company/" + path;
	}

}
